package sm.upgrade;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sm.upgrade.api.UpgradePhase;

public class SqlmappingUpgrade implements UpgradePhase {

	private static Logger logger = LoggerFactory.getLogger(UpgradePhase.class);

	private HibernateTemplate template;

	public void setTemplate(HibernateTemplate template) {
		this.template = template;
	}

	@Override
	public void upgrade() {
		logger.info("Start fixing sqlmapping revision.");
		String[] filenames = new String[] { "Object", "Process", "States",
				"code", "displayevent", "displayoption", "enclapplication",
				"enclapplrev", "format", "link", "menu" };
		for (String filename : filenames) {
			List<Object> sqlmappings = template.find(
					"from sqlmapping where filename = ?", filename);
			if (sqlmappings != null && !sqlmappings.isEmpty()) {
				for (Object sqlmapping : sqlmappings) {
					template.delete("sqlmapping", sqlmapping);
				}
			}
		}

		logger.info("  Finish fixing sqlmapping revision.");
	}

}
